﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKExcelFormTool.Creator;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;

namespace YJKExcelFormTool.Forms
{
	public partial class DoorWindowDetailedStatementForm : System.Windows.Forms.Form
	{
		public TableExportKit ExportKit
		{
			get
			{
				return this.m_ExportKit;
			}
		}

		public bool IsDrawingToRevit
		{
			get
			{
				return this.m_IsDrawingToRevit;
			}
		}

		public TableInfos TheTableInfos
		{
			get
			{
				return this.m_CommonForm.TheTableInfos;
			}
		}

		public DoorWindowDetailedStatementForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.InitData();
		}

		private void DoorWindowDetailedStatementForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				string message = "";
				if (!this.SaveData(ref message))
				{
					YJKMessageBox.Show(message);
					e.Cancel = true;
					return;
				}
			}
			else if (base.DialogResult == DialogResult.No)
			{
				e.Cancel = true;
			}
		}

		private void btDrawing_Click(object sender, EventArgs e)
		{
			if (this.m_CommonForm.TheTableInfos.IsNull())
			{
				YJKMessageBox.Show("当前表头数据为空，请设置表头数据后，重新绘制。");
				base.DialogResult = DialogResult.No;
				return;
			}
			base.DialogResult = DialogResult.OK;
			this.m_IsDrawingToRevit = true;
		}

		private void btExport_Click(object sender, EventArgs e)
		{
			if (this.m_CommonForm.TheTableInfos.IsNull())
			{
				YJKMessageBox.Show("当前表头数据为空，请设置表头数据后，重新输出。");
				base.DialogResult = DialogResult.No;
				return;
			}
			string fontName = this.TheTableInfos.FontName();
			int fontSize = (int)(this.TheTableInfos.FontHeight() / 0.353);
			using (ExportExcelSettingForm exportExcelSettingForm = new ExportExcelSettingForm(ExportExcelSettingForm.ExportType.DoorWindowDetailedStatement, fontName, fontSize))
			{
				exportExcelSettingForm.SetPageRowInfo(this.m_CommonForm.TheTableInfos.Head.RowSize, this.m_CommonForm.TheTableInfos.Head.TableSourceInfo.BadyEndRowNumber);
				if (exportExcelSettingForm.ShowDialog(this) != DialogResult.OK)
				{
					base.DialogResult = DialogResult.No;
					return;
				}
				TableExportKit.FontSetting fontSetting = default(TableExportKit.FontSetting);
				fontSetting.name = exportExcelSettingForm.FontName;
				fontSetting.size = exportExcelSettingForm.FontSize;
				fontSetting.alignmentH = (TableExportKit._tagHorizontalAlignment)exportExcelSettingForm.FontHorizontalalignment;
				fontSetting.alignmentV = (TableExportKit._tagVerticalAlignment)exportExcelSettingForm.FontVerticalAlignment;
				this.m_CommonForm.TheTableInfos.Head.TableSourceInfo.BadyEndRowNumber = exportExcelSettingForm.EndRowInfo;
				this.m_ExportKit = new TableExportKit(this.m_CommonForm.TheTableInfos, exportExcelSettingForm.UsingDefaultFontSetting, fontSetting);
			}
			base.DialogResult = DialogResult.OK;
			this.m_IsDrawingToRevit = false;
		}

		private void InitData()
		{
			string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\DoorWindowDetailedStatementSetting.xml";
			string text = Product.WorkingLocation + "DoorWindowDetailedStatementSetting.xml";
			if (!File.Exists(text))
			{
				this.CopyFile(sourceFileName, text);
			}
			string sourceFileName2 = AssistFunc.GetAppRootPath() + "\\Data\\标准门窗表.xml";
			string text2 = Product.WorkingLocation + "标准门窗表.xml";
			if (!File.Exists(text2))
			{
				this.CopyFile(sourceFileName2, text2);
			}
			List<string> list = new List<string>();
			list.Add("族名");
			list.Add("编号");
			list.Add("规格");
			list.Add("宽度");
			list.Add("高度");
			list.Add("楼层");
			list.Add("数量");
			list.Add("单位");
			list.Add("备注");
			Dictionary<string, string> dict = this.InitColumnDict();
			this.m_CommonForm = new TableCommonForm(this.m_Revit, text, text2, "标准门窗表", "门窗明细表", list, dict);
			this.m_CommonForm.TopLevel = false;
			this.m_CommonForm.Dock = DockStyle.Fill;
			this.m_CommonForm.FormBorderStyle = FormBorderStyle.None;
			this.pnCommnForm.Controls.Add(this.m_CommonForm);
			this.m_CommonForm.Show();
		}

		private void CopyFile(string sourceFileName, string destFileName)
		{
			try
			{
				File.Copy(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		private bool SaveData(ref string message)
		{
			if (!this.m_CommonForm.SaveData(ref message))
			{
				return false;
			}
			DoorWindowDetailedStatementCreator doorWindowDetailedStatementCreator = new DoorWindowDetailedStatementCreator(this.m_Revit);
			this.m_CommonForm.TheTableInfos.Body = doorWindowDetailedStatementCreator.CreateTableBody(this.m_CommonForm.TheTableInfos.EffectiveColumnParameterList());
			return true;
		}

		private Dictionary<string, string> InitColumnDict()
		{
			return new Dictionary<string, string>
			{
				{
					"族名",
					"族名"
				},
				{
					"编号",
					"编号"
				},
				{
					"规格",
					"规格"
				},
				{
					"宽度",
					"宽度"
				},
				{
					"高度",
					"高度"
				},
				{
					"楼层",
					"楼层"
				},
				{
					"数量",
					"数量"
				},
				{
					"单位",
					"单位"
				},
				{
					"备注",
					"备注"
				}
			};
		}

		private ExternalCommandData m_Revit;

		private TableCommonForm m_CommonForm;

		private bool m_IsDrawingToRevit;

		private TableExportKit m_ExportKit;
	}
}
